package com.bardsoftware.foronuvolo.data;

import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.NotPersistent;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.bardsoftware.foronuvolo.server.UserService;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Message {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key myID;

    @Persistent
    private Discussion myDiscussion;

    @Persistent
    private String myUserID;

    @Persistent(defaultFetchGroup = "true")
    private Text myText;

    @Persistent
    private Date myCreation;

    @Persistent
    private String mySourceUrl;

    @Persistent
    private Long sourceForgeID;

    @NotPersistent
    private ForumUser myForumUser;

    public Message() {
    }
    public Message(Discussion d, ForumUser user, String text, Date creation) {
        if (d != null) {
            KeyFactory.Builder keyBuilder = new KeyFactory.Builder(Discussion.class.getSimpleName(), d.getID());
            keyBuilder.addChild(Message.class.getSimpleName(), d.getReplyCount() + 1);
            myID = keyBuilder.getKey();
        }
        myUserID = user.getID();
        myForumUser = user;
        myText = new Text(text);
        myCreation = creation;
    }
    public Message(Discussion d, Message m) {
        this(d, m.getForumUser(), m.getText(), m.myCreation);
        this.sourceForgeID = m.sourceForgeID;
        this.mySourceUrl = m.mySourceUrl;
    }
    public Key getID() {
        return myID;
    }

    public String getText() {
        return myText.getValue();
    }

    public String getUserID() {
        return myUserID;
    }
    String getUserRefID() {
        return getForumUser().getRefID();
    }

    public Date getCreation() {
        return myCreation;
    }

    public String getUserDisplayName() {
        return getForumUser().getDisplayName();
    }

    private ForumUser getForumUser() {
        if (myForumUser == null) {
            myForumUser = UserService.fetchUserByID(getUserID());
            if (myForumUser == null) {
                myForumUser = ForumUser.ANONYMOUS;
            }
        }
        return myForumUser;
    }
    public void setSourceUrl(String url) {
        mySourceUrl = url;
    }

    public boolean isTrusted() {
        return mySourceUrl != null;
    }

    public Discussion getDiscussion() {
        return myDiscussion;
    }
    public void setSourceForgeID(Long id) {
        this.sourceForgeID = id;
    }
    public Long getSourceForgeID() {
        return sourceForgeID;
    }

    public static void deleteAll() {
        PersistenceManager pm = PMF.getFactory().getPersistenceManager();
        try {
            Query q = pm.newQuery(Message.class);
            q.deletePersistentAll();
        }
        finally {
            pm.close();
        }
    }

    public static Message findBySourceForgeID(Long id) {
        PersistenceManager pm = PMF.getFactory().getPersistenceManager();
        try {
            Query discussionsQuery = pm.newQuery(Message.class);
            discussionsQuery.setRange(0, 1);
            discussionsQuery.setFilter("sourceForgeID == paramID");
            discussionsQuery.declareParameters("Long paramID");
            List<Message> result = (List<Message>) discussionsQuery.execute(id);
            return result.isEmpty() ? null : pm.detachCopy(result.get(0));
        }
        finally {
            pm.close();
        }
    }
}
